//
//  FootRefreshViewController.m
//  TableViewFootRefresh
//
//  Created by Apple on 17/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMFootRefreshViewController.h"

//  定义底部刷新枚举类型
typedef enum : NSUInteger {
    Normal,
    Pulling,
    Refreshing,
} RefreshType;

@interface HMFootRefreshViewController ()

//  底部刷新视图
@property (nonatomic, strong) UIView *refreshView;
//  当前刷新状态
@property (nonatomic, assign) RefreshType refreshType;
//  上一次刷新状态
@property (nonatomic, assign) RefreshType lastRefreshType;
//
@end

@implementation HMFootRefreshViewController

- (void)loadView
{
    [super loadView];
    self.tableViewStyle = UITableViewStylePlain;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
}

- (UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:self.tableViewStyle];
        //  添加底部刷新视图
//        [_tableView addSubview:self.refreshView];
        
        //  监听tableview的滚动
//        [_tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _tableView;
}

- (UIView *)refreshView
{
    if (!_refreshView)
    {
        _refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
        //  风火轮
        UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        indicatorView.tag = 101;
        //  消息label
        UILabel *msgLabel = [[UILabel alloc] init];
        msgLabel.tag = 102;
        msgLabel.text = @"上拉加载";
    
        [_refreshView addSubview:indicatorView];
        [_refreshView addSubview:msgLabel];
        //  设置约束
        indicatorView.translatesAutoresizingMaskIntoConstraints = false;
        msgLabel.translatesAutoresizingMaskIntoConstraints = false;
        
        [_refreshView addConstraint: [NSLayoutConstraint constraintWithItem:indicatorView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_refreshView attribute:NSLayoutAttributeCenterX multiplier:1 constant:-40]];
        [_refreshView addConstraint: [NSLayoutConstraint constraintWithItem:indicatorView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_refreshView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        
        [_refreshView addConstraint: [NSLayoutConstraint constraintWithItem:msgLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:indicatorView attribute:NSLayoutAttributeRight multiplier:1 constant:5]];
        
        [_refreshView addConstraint: [NSLayoutConstraint constraintWithItem:msgLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:indicatorView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    }
    return _refreshView;
}

- (void)setRefreshType:(RefreshType)refreshType
{
    //  设置数值
    _refreshType = refreshType;
    //  获取风火轮
    UIActivityIndicatorView *indicatorView = [self.refreshView viewWithTag:101];
    UILabel *msgLabel = [self.refreshView viewWithTag:102];
    
    switch (refreshType)
    {
        case Normal:
            msgLabel.text = @"上拉加载";
            //  停止风火轮
            [indicatorView stopAnimating];
            
            if (self.lastRefreshType == Refreshing)
            {
                //  设置原始位置
                self.tableView.contentInset = UIEdgeInsetsZero;
                //  设置底部刷新控件的位置
                self.refreshView.frame = CGRectMake(0, self.tableView.contentSize.height, self.tableView.frame.size.width, 50);
            }
            break;
        case Pulling:
            msgLabel.text = @"松手就刷新";
            break;
        case Refreshing:
            //  设置文字
            msgLabel.text = @"正在刷新";
            //  开启风火轮
            [indicatorView startAnimating];
            //  设置停留
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.refreshView.frame.size.height, 0);
            //  底部刷新
            [self startFootRefreshing];
            
            break;
    }
}

#pragma mark -- kvo监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    //  设置临界点
    CGFloat maxY = self.tableView.contentSize.height + self.refreshView.frame.size.height;
    //  垂直偏移量
    CGFloat conentOffSetY = self.tableView.contentOffset.y;
    //  滚动偏移量
    CGFloat scrollOffSetY = conentOffSetY + self.tableView.frame.size.height;
    
    //  判断是否是在拖动
    if ([self.tableView isDragging])
    {
        if (scrollOffSetY > maxY && self.refreshType == Normal)
        {
            //  记录上次刷新状态
            self.lastRefreshType = Pulling;
            self.refreshType = Pulling;
            NSLog(@"pulling");
        } else if (scrollOffSetY <= maxY && self.refreshType == Pulling) {
            //  记录上次刷新状态
            self.lastRefreshType = normal;
            self.refreshType = Normal;
            NSLog(@"normal");
        }
    }
    else
    {
        if (self.refreshType == Pulling)
        {
            //  记录上次刷新状态
            self.lastRefreshType = Refreshing;
            self.refreshType = Refreshing;
            NSLog(@"Refreshing");
        }
    }
}

- (void)endRefresh
{
    self.refreshType = Normal;
}

#pragma mark --开始刷新
- (void)startFootRefreshing {

    //  开始刷新,空实现
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    //  如果没有加载回来数据tableView的contentSize的大小是0,0,设置frame的大小就可以了
    if (self.tableView.contentSize.height < self.tableView.frame.size.height) {
        //  默认位置
        self.refreshView.frame = CGRectMake(0, self.tableView.frame.size.height, self.tableView.frame.size.width, 50);
        
    } else {
        //  默认位置
        self.refreshView.frame = CGRectMake(0, self.tableView.contentSize.height, self.tableView.frame.size.width, 50);
    }
}

- (void)dealloc
{
    //  移除kvo
    [self.tableView removeObserver:self forKeyPath:@"contentOffset"];
}

@end
